package com.dbsop.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.dbsop.common.enums.TypeEnum;
import com.dbsop.domain.info.EsFiledInfo;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 返回值处理
 */
public class ValuesUtils {


    /**
     *  引用对象不进行设置, 只设置基础类型
     * @param esFiledInfo
     * @param value
     */
    public static void assignment(EsFiledInfo esFiledInfo, Object obj, Object value) throws IllegalAccessException {
        Class fieldType = esFiledInfo.getFieldType();
        Field field = esFiledInfo.getField();
        TypeEnum anEnum = TypeEnum.getTypeEnumByClass(fieldType);
        if (value == null)return;

        switch (anEnum){
            case BYTE:
                field.set(obj,new BigDecimal(value.toString()).byteValue());
                break;
            case BOOLEAN:
                field.set(obj,Boolean.valueOf(value.toString()));
                break;
            case LONG:
                field.set(obj,new BigDecimal(value.toString()).longValue());
                break;
            case INTEGER:
                field.set(obj,new BigDecimal(value.toString()).intValue());
                break;
            case FLOAT:
                field.set(obj,new BigDecimal(value.toString()).floatValue());
                break;
            case DOUBLE:
                field.set(obj,new BigDecimal(value.toString()).doubleValue());
                break;
            case STRING:
                if (value!=null){
                    field.set(obj,value.toString());
                }
                break;
            case LIST:
                if (value!=null){
                    Class typez = null;
                    Type fc = field.getGenericType();
                    if (fc != null && fc instanceof ParameterizedType) {
                        ParameterizedType pt = (ParameterizedType) fc;
                        Type[] types = pt.getActualTypeArguments();
                        if (types.length == 1 && types[0] instanceof Class){
                            typez = (Class) types[0];
                        }
                    }
                    if (typez == null){
                        field.set(obj,new JSONArray((List<Object>) value).toJavaList(Object.class));
                    }else {

                        field.set(obj, new JSONArray((List<Object>) value).toJavaList(typez));
                    }
                }
                break;
            case OBJECT:
                if (value!=null){
                    Class<?> declaringClass = field.getDeclaringClass();
                    field.set(obj, JSON.parseObject(value.toString(),declaringClass));
                }
                break;
            default:
                field.set(obj,value);
                break;
        }
    }


    /**
     * 字段赋值
     * @param fieldList
     * @param obj
     * @param sourceAsMap
     * @throws IllegalAccessException
     */
    public static void fieldAssignment(List<EsFiledInfo> fieldList,Object obj, Map<String, Object> sourceAsMap) throws IllegalAccessException {
        for (EsFiledInfo esFiledInfo : fieldList) {
            esFiledInfo.getField().setAccessible(true);
            assignment(esFiledInfo,obj,sourceAsMap.get(esFiledInfo.getFieldName()));
        }
    }

    /**
     * 字段赋值1
     * @param fieldMap
     * @param obj
     * @param sourceAsMap
     * @throws IllegalAccessException
     */
    public static void fieldAssignment(Map<String,EsFiledInfo> fieldMap,Object obj, Map<String, Object> sourceAsMap) throws IllegalAccessException {
        for (String key:sourceAsMap.keySet()){
            Object value = sourceAsMap.get(key);

            EsFiledInfo esFiledInfo = fieldMap.get(key);
            if (esFiledInfo == null || value == null)continue;
            esFiledInfo.getField().setAccessible(true);
            // 增加类型兼容
            assignment(esFiledInfo,obj,value);
        }
    }

}
